package com.beiding.workordersystem.orders.looporder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.beiding.workordersystem.common.service.BaseService;
import com.beiding.workordersystem.common.standard.E;
import com.beiding.workordersystem.common.utils.QueryUtils;
import com.beiding.workordersystem.orders.looporder.dao.CompanyDao;
import com.beiding.workordersystem.orders.looporder.dao.CustomCompanyItemDao;
import com.beiding.workordersystem.orders.looporder.entity.Company;
import com.beiding.workordersystem.orders.looporder.entity.CustomCompanyItem;
import com.beiding.workordersystem.orders.looporder.query.CompanyQuery;
import com.beiding.workordersystem.orders.looporder.service.CompanyService;
import com.beiding.workordersystem.system.service.VariableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Random;

@Service
public class CompanyServiceImpl extends BaseService implements CompanyService {

    @Autowired
    private CompanyDao companyDao;

    @Override
    public IPage page(IPage toIPage, CompanyQuery query) {

        LambdaQueryWrapper<Company> companyLambdaQueryWrapper = QueryUtils.getQuery(Company.class).eq(Company::getDeal, true);


        if (notEmpty(query.getTaxNature())) {
            companyLambdaQueryWrapper.eq(Company::getTaxNature, query.getTaxNature());
        }

        if (notEmpty(query.getRank())) {
            companyLambdaQueryWrapper.eq(Company::getRank, query.getRank());
        }

        if (notEmpty(query.getAccountType())) {
            companyLambdaQueryWrapper.eq(Company::getAccountType, query.getAccountType());
        }
        if (notEmpty(query.getCompanyName())) {
            companyLambdaQueryWrapper.like(Company::getName, query.getCompanyName());
        }

        if (query.getHasBank()) {
            companyLambdaQueryWrapper.eq(Company::getHasBank, query.getHasBank());
        }

        if (query.getHasSocialSecurity()) {
            companyLambdaQueryWrapper.eq(Company::getHasSocialSecurity, query.getHasSocialSecurity());
        }

        if (query.getHasAccumulationFundAccount()) {
            companyLambdaQueryWrapper.eq(Company::getHasAccumulationFundAccount, query.getHasAccumulationFundAccount());
        }

        return companyDao.selectPage(toIPage, companyLambdaQueryWrapper);
    }

    @Override
    public void delete(String[] ids) {
        for (String id : ids) {
            companyDao.deleteById(id);
        }
    }

    @Override
    public void workOff(String[] ids) {
        for (String id : ids) {
            Company company = companyDao.selectById(id);
            E.notNull(company, "公司不存在");
            company.setWorkOff(true);
            companyDao.updateById(company);
        }
    }

    @Override
    public Company details(String id) {
        return companyDao.selectById(id);
    }

    @Override
    public void save(Company company) {
        //清空
        if (!company.getHasAccumulationFundAccount()) {
            company.setAccumulationFundAccount("");
            company.setAccumulationFundPassword("");
        }

        if (!company.getHasBank()) {
            company.setBank("");
        }

        if (!company.getHasSocialSecurity()) {
            company.setSocialSecurityAccount("");
            company.setSocialSecurityPassword("");
        }

        if (!company.getHasTaxControlDisk()) {
            company.setTaxControlDiskAccount("");
            company.setTaxControlDiskPassword("");
        }

        if (fillId(company)) {
            if (company.getDeal() == null) {
                company.setDeal(true);
            }
            company.setCode(createCompanyCode());
            init(company);
            companyDao.insert(company);
        } else {
            companyDao.updateById(company);
        }
    }

    @Autowired
    private CustomCompanyItemDao customCompanyItemDao;

    @Override
    public List<CustomCompanyItem> listCustom() {
        return customCompanyItemDao.selectList(new QueryWrapper<>());
    }

    @Override
    public void saveCustom(CustomCompanyItem item) {
        if (fillId(item)) {
            E.isFalse(customCompanyItemDao.exist(QueryUtils.getQuery(item).eq(CustomCompanyItem::getName, item.getName())), "字段重复:" + item.getName());
            item.setCode(createCode());
            customCompanyItemDao.insert(item);
        } else {
            customCompanyItemDao.updateById(item);
        }
    }

    synchronized
    private String createCode() {
        return "p_" + System.currentTimeMillis();
    }

    @Override
    public void deleteCustom(String[] list) {
        for (String s : list) {
            customCompanyItemDao.deleteById(s);
        }
    }


    @Autowired
    private VariableService variableService;

    private DecimalFormat decimalFormat = new DecimalFormat("00000");

    @Transactional
    String createCompanyCode() {
        String max_company_code = variableService.getValue("max_company_code");
        if (max_company_code == null) {
            variableService.set("max_company_code", "00001");
            return "00001";
        } else {
            Integer max = new Integer(max_company_code);
            max++;
            String format = decimalFormat.format(max);
            variableService.set("max_company_code", format);
            return format;
        }

    }

    public static void main(String[] args) {

        char[] chars = new char[62];

        for (int i = 0; i < 26; i++) {
            chars[i] = (char) ('a' + i);
        }

        for (int i = 0; i < 26; i++) {
            chars[i+26] = (char) ('A' + i);
        }

        for (int i = 0; i < 10; i++) {
            chars[i + 52] = (char) ('0' + i);
        }

        StringBuilder builder = new StringBuilder();

        Random random = new Random();
        for (int i = 0; i < 20; i++) {
            builder.append(chars[random.nextInt(62)]);
        }
        System.out.println(builder.toString());
    }


}
